<?php
$DatesRequired = false;
$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

$priceSystem = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit);
$peopleMaximums = $priceSystem->getPeopleMaximums($this->unit->id);

if (isset($criteria) && (isset($criteria->start_datetime) && isset($criteria->end_datetime))) {

    /**
     * This uses the checks in the price calculator to check the start day and
     * minimum number of days are ok.
     */
    if ($criteria->start_datetime && $criteria->end_datetime) {
        $period = new RM_Reservation_Period(
            new RM_Date(strtotime($criteria->start_datetime)),
            new RM_Date(strtotime($criteria->end_datetime))
        );
    } else {
        $period = new RM_Reservation_Period(
            new RM_Date(strtotime($criteria->start_datetime)),
            new RM_Date(strtotime($criteria->end_datetime))
        );
    }

    $persons = new RM_Reservation_Persons(array("adults"=>$criteria->adults,"children"=>$criteria->children,"infants"=>$criteria->infants));

    $information = new RM_Prices_Information($this->unit, $period, $persons);

    try {
        $showAddToCart = $priceSystem->getTotalUnitPrice($information);
        $boardTypes = $priceSystem->getBoardTypes($this->unit->getID(),$period);
    } catch (Exception $e) {
        $DatesRequired = true;
    }

    /**
     * this checks if the unit has any partial reservations
     * This is only used when the flexible criteria option is selected.
     */
    if ($criteria->flexible && !$DatesRequired){
        $unitDAO = new RM_Units();
        $reservations = $unitDAO->isFlexibleAvailable($this->unit, $period);

        if (!empty($reservations)){
            $DatesRequired = true;
        }

    }
}

$panelsettings = Zend_Json::decode($this->panel->settings); //panel settings

// handle groups
$group_id = $this->unit->getGroupID();
if ( !$group_id ){
    $uid = $this->unit->getId();
    $isGroup = false;
} else {
    $uid = $group_id;
    $isGroup = true;
}
?>

<script type="text/javascript">

var adults = <?php echo (int)$peopleMaximums['max_adults'];?>;
var children = <?php echo (int)$peopleMaximums['max_children'];?>;
var infants = <?php echo (int)$peopleMaximums['max_infants'];?>;

// used to store locally the selection
var adultsSelection;
var childrenSelection;
var infantsSelection;
var boardtypeselect;

</script>

<div id="peopleselection_<?php echo $uid; ?>" style="display:none;">
    <!-- maximum people selection -->
    <b><?php echo $this->_translate->_('User.Hospitality.Main','SelectedPeople');?></b>
    <table width="220" border="0" cellpadding="6">
        <tr>
            <td><?php echo $this->_translate->_('User.DatePicker','Adults');?></td>
            <td>
                <select id="adults_<?php echo $uid; ?>" onchange="updatePersonSelection('adultsSelection', this.value);">
                    <?php
                    for ($i = 1; $i <= (int)$peopleMaximums['max_adults']; $i++) {
                        if (isset($criteria)) {
                            if ($criteria->adults==$i){
                                // render the selected
                                ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        } else {
                            ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>
    <?php if ((int)$peopleMaximums['max_children']>0){?>
        <tr>
            <td><?php echo $this->_translate->_('User.DatePicker','Children');?></td>
            <td>
              <select id="children_<?php echo $uid; ?>" class="RM_hospitality_feild" onchange="updatePersonSelection('childrenSelection', this.value);">
                    <?php
                    for ($i = 0; $i <= (int)$peopleMaximums['max_children']; $i++) {
                        if (isset($criteria)) {
                            if ($criteria->children==$i){
                                // render the selected
                                ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        } else {
                            ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>
    <?php }
    if ((int)$peopleMaximums['max_infants']>0){?>
        <tr>
            <td><?php echo $this->_translate->_('User.DatePicker','Infants');?></td>
            <td>
                <select id="infants_<?php echo $uid; ?>" class="RM_hospitality_feild" onchange="updatePersonSelection('infantSelection', this.value);">
                    <?php
                    for ($i = 0; $i <= (int)$peopleMaximums['max_infants']; $i++) {
                        if (isset($criteria)) {
                            if ($criteria->infants==$i){
                                // render the selected
                                ?><option value="<?php echo $i;?>" selected><?php echo $i;?></option><?php
                            } else {
                                ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                            }
                        } else {
                            ?><option value="<?php echo $i;?>"><?php echo $i;?></option><?php
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>
    <?php }?>
        <tr>
            <td><?php echo $this->_translate->_('User.DatePicker','BoardType');?></td>
            <td>
                <select id="boardtypeselect" onchange="updatePersonSelection('boardtypeselect', this.value);">
                    <?php
                    if (isset($boardTypes)){
                        foreach ($boardTypes as $board){
                            echo "<option value='".$board['board_type']."'>".$this->_translate->_('User.DatePicker',$board['board_type'])."</option>";
                        }
                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <span class="RM_unit_list_cartbutton">
                <input type="button" onclick="rm_hospitality_unit_list_addtocart('<?php echo $uid; ?>')" img="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/large/addcart.png" />
                </span>
            </td>
        </tr>
    </table>
</div>

<div id="<?php echo $this->panel->getDivID() ?>" >
    <div class="RM_unit_list_cartandbuynow_container" style="float:<?php echo $panelsettings['align'];?>">
        <span class="RM_unit_list_cartbutton">
            <?php if (!$DatesRequired) {?>
                <input type="button" onclick="rm_hospitality_call_select()" img="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/large/addcart.png" />
            <?php } else { ?>
                <img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/large/addcart_disabled.png" border="0" alt="<?php echo $this->_translate->_('User.Unit.List','AddtoCartNotAvailable');?>">
            <?php } ?>
        </span>
        <span class="RM_unit_list_booknowbutton">
            <input type="button" value="<?php echo $this->_translate->_('User.Unit.List','MoreInfo');?>" onclick="window.location.href='<?php echo RM_Environment::getInstance()->getRouter()->_('Unit', 'details', array('unit_id' => $uid, 'isGroup'=>$isGroup)) ?>'" />
        </span>
    </div>
</div>
<div id="RM_Hospitality_list_add_to_cart_<?php echo $uid; ?>"></div>
<div class="RM_clear"></div>

<script type="text/javascript">

function updatePersonSelection(variable,value){
    if (variable == "adultsSelection") { adultsSelection = value};
    if (variable == "childrenSelection") { childrenSelection = value};
    if (variable == "infantSelection") { infantSelection = value};
    if (variable == "boardtypeselect") { boardtypeselect = value};
}

function rm_hospitality_call_select(){
    if ("<?php echo $criteria->adults;?>" === "" && "<?php echo $criteria->start_datetime; ?>" !== ""){
        rm_hospitality_showPersonSelection('<?php echo $uid; ?>', "<?php echo $this->unit->name;?>");
    } else {
        rm_hospitality_unit_list_addtocart('<?php echo $uid; ?>');
    }
}

</script>

<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/views/user/scripts/Unit/list/hospitality_cartbook_btn_list.js'></script>